package com.simou.controller;

import com.simou.pojo.User;
import com.simou.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @Autowired
    UserServiceImpl userService;

    @RequestMapping("/")
    public String login(){
        return "forward:/login.jsp";
    }

    @RequestMapping("/login.do")
    public String verityLogin(String userCode, String userPassword, Model model, HttpSession session){
        System.out.println("userCode:"+userCode);
        System.out.println("userPassword:"+userPassword);
        User user = userService.getUser(userCode, userPassword);
        System.out.println(user);
//      用户码密码正确
        if (user!=null){
            session.setAttribute("userSession",user);
            return "frame";
        }
//      用户码密码错误
        model.addAttribute("error","用户账号或密码错误");
        return "../../login";
    }

    @RequestMapping("/jsp/logout.do")
    public String logOut(HttpSession session){
        session.removeAttribute("userSession");
        return "redirect:/";
    }
}
